#include "CentralTabCloseButton.h"

#include <QDebug>
#include <QPainter>
#include <QStyleOption>
#include <QTabBar>

CentralTabCloseButton::CentralTabCloseButton(QWidget *parent) : CToolButton(parent) {
    // Set icon by stylesheet
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    setCheckable(true);
}

CentralTabCloseButton::~CentralTabCloseButton() {
}

void CentralTabCloseButton::paintEvent(QPaintEvent *e) {
    if (const QTabBar *bar = qobject_cast<const QTabBar *>(parent())) {
        int index = bar->currentIndex();
        if (bar->tabButton(index, QTabBar::RightSide) == this) {
            if (isChecked()) {
                setChecked(false);
                return;
            }
        } else {
            if (!isChecked()) {
                setChecked(true);
                return;
            }
        }
    }
    CToolButton::paintEvent(e);
}
